// Simple Note Taking Application
const colors = require('colors');

class NoteApp {
  constructor() {
    this.notes = [];
    this.currentId = 1;
  }

  // Add a new note
  addNote(title, content) {
    const note = {
      id: this.currentId++,
      title: title,
      content: content,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
    };
    this.notes.push(note);
    console.log(`Note "${title}" added successfully!`.green);
    return note;
  }

  // Get all notes
  getAllNotes() {
    return this.notes;
  }

  // Get note by ID
  getNoteById(id) {
    return this.notes.find(note => note.id === id);
  }

  // Update a note
  updateNote(id, title, content) {
    const note = this.getNoteById(id);
    if (note) {
      note.title = title || note.title;
      note.content = content || note.content;
      note.updatedAt = new Date().toISOString();
      console.log(`Note "${note.title}" updated successfully!`.blue);
      return note;
    } else {
      console.log(`Note with ID ${id} not found.`);
      return null;
    }
  }

  // Delete a note
  deleteNote(id) {
    const index = this.notes.findIndex(note => note.id === id);
    if (index !== -1) {
      const deletedNote = this.notes.splice(index, 1)[0];
      console.log(`Note "${deletedNote.title}" deleted successfully!`.red);
      return deletedNote;
    } else {
      console.log(`Note with ID ${id} not found.`.yellow);
      return null;
    }
  }

  // Search notes by title or content
  searchNotes(query) {
    const searchTerm = query.toLowerCase();
    return this.notes.filter(
      note =>
        note.title.toLowerCase().includes(searchTerm) ||
        note.content.toLowerCase().includes(searchTerm)
    );
  }

  // Display all notes
  displayNotes() {
    if (this.notes.length === 0) {
      console.log('No notes available.');
      return;
    }

    console.log('\n=== All Notes ==='.cyan.bold);
    this.notes.forEach(note => {
      console.log(`\nID: ${note.id}`.magenta);
      console.log(`Title: ${note.title}`.white.bold);
      console.log(`Content: ${note.content}`.gray);
      console.log(`Created: ${new Date(note.createdAt).toLocaleString()}`);
      console.log(`Updated: ${new Date(note.updatedAt).toLocaleString()}`);
      console.log('---');
    });
  }
}

// Example usage
if (require.main === module) {
  const noteApp = new NoteApp();

  // Add some sample notes
  noteApp.addNote('学习计划', '今天要学习Vue.js和Node.js');
  noteApp.addNote('购物清单', '牛奶、面包、鸡蛋、水果');
  noteApp.addNote('工作任务', '完成项目文档，准备会议材料');

  // Display all notes
  noteApp.displayNotes();

  // Search for notes
  console.log("\n=== Search Results for '学习' ===".cyan.bold);
  const searchResults = noteApp.searchNotes('学习');
  searchResults.forEach(note => {
    console.log(`Found: ${note.title} - ${note.content}`.green);
  });

  // Update a note
  noteApp.updateNote(
    1,
    '学习计划 - 更新',
    '今天要学习Vue.js、Node.js和cnpm的使用'
  );

  // Display updated notes
  noteApp.displayNotes();
}

module.exports = NoteApp;
